package pdf;
/** 
* @author 杨迅
* @number 16301024
* @time 2018年4月25日 下午5:55:27
* FileName:Tuo.java
* Description:
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileSystemView;

import com.lowagie.text.DocumentException;


public class PDFtoolsFrame extends JFrame{
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int MAX_NUMBER_OF_FILE = 100;
	//合并PDF
	private int index = 0;
	private String[] filepath = new String[MAX_NUMBER_OF_FILE];
	private String[] fileName = new String[MAX_NUMBER_OF_FILE];
	//拆分PDF
	private int index1 = 0;
	private String[] filepath1 = new String[MAX_NUMBER_OF_FILE];
	private String[] fileName1 = new String[MAX_NUMBER_OF_FILE];
	//PDF转图片
	private int index2 = 0;
	private String[] filepath2 = new String[MAX_NUMBER_OF_FILE];
	private String[] fileName2 = new String[MAX_NUMBER_OF_FILE];
	//图片转PDF
	private int index3 = 0;
	private String[] filepath3 = new String[MAX_NUMBER_OF_FILE];
	private String[] fileName3 = new String[MAX_NUMBER_OF_FILE];
//	private JLabel lblplay;
//	private JScrollPane ScrollList;
	private JList<String> list = new JList<String>();
	private JList<String> list1 = new JList<String>();
	private JList<String> list2 = new JList<String>();
	private JList<String> list3 = new JList<String>();
//	private JButton btnSelectedAll;
//	private JButton btnAddFile;
//	private JButton btnDeleteFile;
//	private JButton btnMoveUp;
//	private JButton btnMoveDown;
//	private JButton btnExport;
	private JButton btnMerge;
	private JButton btnSplit;
	private JButton btnToImage;
	private JButton btnToPDF;
	private JPanel mainPane;
	private JPanel mergePane;
	private JPanel splitPane;
	private JPanel toImagePane;
	private JPanel toPDFPane;
	private int judge = 1;
	
    public PDFtoolsFrame()
     {
        mainPane =new JPanel();
        getContentPane().add(mainPane,BorderLayout.CENTER);
        setSize(400,410);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(400,200);
        setTitle("PDFTools");
        mainPane.setLayout(null);
        drag();
        
        
        btnMerge = new JButton("PDF合并");
		btnMerge.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.add(btnMerge);
				mainPane.add(btnSplit);
				mainPane.add(btnToImage);
				mainPane.add(btnToPDF);
				//mainPane.add(mergePane);
				mergePDF();
				judge = 1;
				mainPane.repaint();
			}
		});
		btnMerge.setBounds(4, 0, 92, 30);
		mainPane.add(btnMerge);
		
		btnSplit = new JButton("PDF拆分");
		btnSplit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.add(btnSplit);
				mainPane.add(btnMerge);
				mainPane.add(btnToImage);
				mainPane.add(btnToPDF);
				splitPDF();
				judge = 2;
				mainPane.repaint();
			}
		});
		btnSplit.setBounds(98, 0, 92, 30);
		mainPane.add(btnSplit);
		
		btnToImage = new JButton("PDF转图片");
		btnToImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.add(btnMerge);
				mainPane.add(btnSplit);
				mainPane.add(btnToImage);
				mainPane.add(btnToPDF);
				//mainPane.add(mergePane);
				pdfToImage();
				judge = 3;
				mainPane.repaint();
			}
		});
		btnToImage.setBounds(192, 0, 92, 30);
		mainPane.add(btnToImage);
		
		btnToPDF = new JButton("图片转PDF");
		btnToPDF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.add(btnMerge);
				mainPane.add(btnSplit);
				mainPane.add(btnToImage);
				mainPane.add(btnToPDF);
				//mainPane.add(mergePane);
				imageToPDF();
				judge = 4;
				mainPane.repaint();
			}
		});
		btnToPDF.setBounds(286, 0, 92, 30);
		mainPane.add(btnToPDF);

		mergePDF();
     }
   
    //PDF合并
    public void mergePDF(){
    	
        mergePane =new JPanel();
        mergePane.setLayout(null);
        mergePane.setBounds(0, 20, 400, 410);
        mainPane.add(mergePane);
        
        JLabel lblplay;
        lblplay = new JLabel("PDF文件列表：");
        lblplay.setFont(new Font("宋体", Font.PLAIN, 15));
        lblplay.setBounds(20, 10, 100, 30);
        mergePane.add(lblplay);
        
        JScrollPane ScrollList;
        ScrollList = new JScrollPane(list);
        ScrollList.setBounds(20, 35, 200, 250);
        ScrollList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        ScrollList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        mergePane.add(ScrollList);
        
        list.setBackground(Color.lightGray);
        // 允许可间断的多选
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                // 获取所有被选中的选项索引
                int[] indices = list.getSelectedIndices();
                // 获取选项数据的 ListModel
                ListModel<String> listModel = list.getModel();
                // 输出选中的选项
                for (int index : indices) {
                    System.out.println("选中: " + index + " = " + listModel.getElementAt(index));
                }
                System.out.println();
            }
        });
//        mergePane.add(list);
        
        //全选按钮
        JButton btnSelectedAll;
        btnSelectedAll = new JButton("全   选");
		btnSelectedAll.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] a  = new int[MAX_NUMBER_OF_FILE];
				for(int i = 0; i < index; i++){
					a[i] = i;
				}
				list.setSelectedIndices(a);
			}
		});
		btnSelectedAll.setBorder(null);
		btnSelectedAll.setBounds(250, 80, 105, 40);
		mergePane.add(btnSelectedAll);
		
		//选择文件按钮
		JButton btnAddFile;
		btnAddFile = new JButton("添加文件");
		btnAddFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int result = 0;
				String path = null;
				JFileChooser fileChooser = new JFileChooser();
				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
				fileChooser.setMultiSelectionEnabled(true);
				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
				fileChooser.setDialogTitle("请选择要上传的文件...");
				fileChooser.setApproveButtonText("确定");
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				result = fileChooser.showOpenDialog(mainPane);
				if (JFileChooser.APPROVE_OPTION == result) {
					File[] files = fileChooser.getSelectedFiles();
					for(File file:files)
                    {
                       filepath[index] = file.getAbsolutePath();
                       fileName[index] = file.getName();
                       index++;
                    }
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index; i++){
						fileNameList[i] = (i+1) + "." + fileName[i];
					}
                   list.setListData(fileNameList);	
		    	   path=fileChooser.getSelectedFile().getPath();
		    	   System.out.println("path: "+path);
				}
			}
		});
		btnAddFile.setBorder(null);
		btnAddFile.setBounds(250, 30, 105, 40);
		mergePane.add(btnAddFile);
		
		//删除文件按钮
		JButton btnDeleteFile;
		btnDeleteFile = new JButton("删除文件");
		btnDeleteFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] selectedIndices = list.getSelectedIndices();
				for(int a : selectedIndices)
					System.out.println(a);
				for(int k = selectedIndices.length - 1; k >= 0; k--){
					System.out.println(fileName[k]);
					for(int i = selectedIndices[k]; i < index-1; i++){
						filepath[i] = filepath[i+1];
						fileName[i] = fileName[i+1];
					}
					index--;
					System.out.println(k);
				}
				String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
				for(int i = 0; i < index; i++){
					fileNameList[i] = (i+1) + "." + fileName[i];
				}
               list.setListData(fileNameList);	
			}
		});
		btnDeleteFile.setBorder(null);
		btnDeleteFile.setBounds(250, 130, 105, 40);
		mergePane.add(btnDeleteFile);
		
		//上移按钮
		JButton btnMoveUp;
		btnMoveUp = new JButton("上   移");
		btnMoveUp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int selectedIndex = list.getSelectedIndex();
				if(selectedIndex > 0){
					String fp = filepath[selectedIndex];
					filepath[selectedIndex] = filepath[selectedIndex - 1];
					filepath[selectedIndex - 1] = fp;
					String fn = fileName[selectedIndex];
					fileName[selectedIndex] = fileName[selectedIndex - 1];
					fileName[selectedIndex - 1] = fn;
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index; i++){
						fileNameList[i] = (i+1) + "." + fileName[i];
					}
	               list.setListData(fileNameList);	
	               list.setSelectedIndex(selectedIndex - 1);
				}
			}
		});
		btnMoveUp.setBorder(null);
		btnMoveUp.setBounds(250, 180, 105, 40);
		mergePane.add(btnMoveUp);
		
		//下移按钮
		JButton btnMoveDown;
		btnMoveDown = new JButton("下   移");
		btnMoveDown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int selectedIndex = list.getSelectedIndex();
				if(selectedIndex != index-1){
					String fp = filepath[selectedIndex];
					filepath[selectedIndex] = filepath[selectedIndex + 1];
					filepath[selectedIndex + 1] = fp;
					String fn = fileName[selectedIndex];
					fileName[selectedIndex] = fileName[selectedIndex + 1];
					fileName[selectedIndex + 1] = fn;
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index; i++){
						fileNameList[i] = (i+1) + "." + fileName[i];
					}
	               list.setListData(fileNameList);	
	               list.setSelectedIndex(selectedIndex + 1);
				}
			}
		});
		btnMoveDown.setBorder(null);
		btnMoveDown.setBounds(250, 230, 105, 40);
		mergePane.add(btnMoveDown);
		
		JLabel lblplay1;
 		lblplay1 = new JLabel("新文件名：");
 		lblplay1.setFont(new Font("宋体", Font.PLAIN, 15));
 		lblplay1.setBounds(20, 295, 100, 30);
 		mergePane.add(lblplay1);
        
        JTextField txtNewName;
        txtNewName = new JTextField();
		txtNewName.setFont(new Font("宋体", Font.PLAIN, 15));
		txtNewName.setBackground(Color.LIGHT_GRAY);
		txtNewName.setForeground(Color.BLACK);
		txtNewName.setBounds(100, 295, 120, 30);
		mergePane.add(txtNewName);

		
		//导出按钮
		JButton btnExport;
        btnExport = new JButton("导出PDF");
        btnExport.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int result = 0;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("选择将合成的新PDF文件导出到");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					String s = fileChooser.getSelectedFile().getAbsolutePath();
 					System.out.println(s);
 					MergeAndSplit m = new MergeAndSplit();
 					int[] selectedIndices = list.getSelectedIndices();
 	 				for(int a : selectedIndices)
 	 					System.out.println(a);
 	 				for(int i = 0; i < selectedIndices.length; i++){
 	 					m.addFile(filepath[selectedIndices[i]]);
 	 				}
 					m.setOutputFilepath(s + "\\" + txtNewName.getText() + ".pdf");
 	 				Thread t = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
		 					 try {
		 						m.mergePDF();
		 					} catch (IOException e1) {
		 						// TODO Auto-generated catch block
		 						e1.printStackTrace();
		 					}
						}
					});
 	 				t.start();
 				}
				
			}
		});
        btnExport.setBorder(null);
        btnExport.setBounds(250, 295, 105, 40);
        mergePane.add(btnExport);
    }

    //PDF拆分
    public void splitPDF(){
    	splitPane =new JPanel();
    	splitPane.setLayout(null);
    	splitPane.setBounds(0, 20, 400, 410);
    	mainPane.add(splitPane);
    	
		 JLabel lblplay;
	     lblplay = new JLabel("PDF文件列表：");
	     lblplay.setFont(new Font("宋体", Font.PLAIN, 15));
	     lblplay.setBounds(20, 10, 100, 30);
	     splitPane.add(lblplay);
	     
	     JScrollPane ScrollList;
	     ScrollList = new JScrollPane(list1);
	     ScrollList.setBounds(20, 35, 340, 130);
	     ScrollList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	     ScrollList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	//         ScrollList.setOpaque(false);
	//         ScrollList.getViewport().setOpaque(false);
	     splitPane.add(ScrollList);
	     
	     list1.setBackground(Color.lightGray);
	     // 允许可间断的多选
	     list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	     list1.addListSelectionListener(new ListSelectionListener() {
	         @Override
	         public void valueChanged(ListSelectionEvent e) {
	             // 获取所有被选中的选项索引
	             int[] indices = list1.getSelectedIndices();
	             // 获取选项数据的 ListModel
	             ListModel<String> listModel = list1.getModel();
	             // 输出选中的选项
	             for (int index : indices) {
	                 System.out.println("选中: " + index + " = " + listModel.getElementAt(index));
	             }
	             System.out.println();
	         }
	     });
	//         mergePane.add(list);
	     
	    //全选按钮
	    JButton btnSelectedAll;
	    btnSelectedAll = new JButton("全   选");
 		btnSelectedAll.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int[] a  = new int[MAX_NUMBER_OF_FILE];
 				for(int i = 0; i < index1; i++){
 					a[i] = i;
 				}
 				list1.setSelectedIndices(a);
 			}
 		});
 		btnSelectedAll.setBorder(null);
 		btnSelectedAll.setBounds(137, 175, 105, 40);
 		splitPane.add(btnSelectedAll);
 		
 		//选择文件按钮
 		JButton btnAddFile;
 		btnAddFile = new JButton("添加文件");
 		btnAddFile.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int result = 0;
 				String path = null;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("请选择要上传的文件...");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					File[] files = fileChooser.getSelectedFiles();
 					for(File file:files)
                     {
                        filepath1[index1] = file.getAbsolutePath();
                        fileName1[index1] = file.getName();
                        index1++;
                     }
 					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
 					for(int i = 0; i < index1; i++){
 						fileNameList[i] = (i+1) + "." + fileName1[i];
 					}
                    list1.setListData(fileNameList);	
 		    	   path=fileChooser.getSelectedFile().getPath();
 		    	   System.out.println("path: "+path);
 				}
 			}
 		});
 		btnAddFile.setBorder(null);
 		btnAddFile.setBounds(27, 175, 105, 40);
 		splitPane.add(btnAddFile);
 		
 		//删除文件按钮
 		JButton btnDeleteFile;
 		btnDeleteFile = new JButton("删除文件");
 		btnDeleteFile.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int[] selectedIndices = list1.getSelectedIndices();
 				for(int a : selectedIndices)
 					System.out.println(a);
 				for(int k = selectedIndices.length - 1; k >= 0; k--){
 					System.out.println(fileName1[k]);
 					for(int i = selectedIndices[k]; i < index1-1; i++){
 						filepath1[i] = filepath1[i+1];
 						fileName1[i] = fileName1[i+1];
 					}
 					index1--;
 					System.out.println(k);
 				}
 				String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
 				for(int i = 0; i < index1; i++){
 					fileNameList[i] = (i+1) + "." + fileName1[i];
 				}
                list1.setListData(fileNameList);	
 			}
 		});
 		btnDeleteFile.setBorder(null);
 		btnDeleteFile.setBounds(247, 175, 105, 40);
 		splitPane.add(btnDeleteFile);
 		
 		JLabel lblplay1;
 		lblplay1 = new JLabel("起始页：");
 		lblplay1.setFont(new Font("宋体", Font.PLAIN, 15));
 		lblplay1.setBounds(40, 235, 70, 30);
        splitPane.add(lblplay1);
        
        JTextField txtFromPage;
        txtFromPage = new JTextField();
		txtFromPage.setFont(new Font("宋体", Font.PLAIN, 15));
		txtFromPage.setBackground(Color.LIGHT_GRAY);
		txtFromPage.setForeground(Color.BLACK);
		txtFromPage.setBounds(100, 235, 80, 30);
		splitPane.add(txtFromPage);
        
        JLabel lblplay2;
        lblplay2 = new JLabel("截止页：");
        lblplay2.setFont(new Font("宋体", Font.PLAIN, 15));
        lblplay2.setBounds(190, 235, 70, 30);
        splitPane.add(lblplay2);

        JTextField txtEndPage;
        txtEndPage = new JTextField();
        txtEndPage.setFont(new Font("宋体", Font.PLAIN, 15));
        txtEndPage.setBackground(Color.LIGHT_GRAY);
        txtEndPage.setForeground(Color.BLACK);
        txtEndPage.setBounds(250, 235, 80, 30);
		splitPane.add(txtEndPage);
		
		JButton btnToPDF;
		btnToPDF = new JButton("导 出 新 PDF");
		btnToPDF.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int result = 0;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("选择将分割的新PDF文件导出到");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					String s = fileChooser.getSelectedFile().getAbsolutePath();
 					System.out.println(s);
 					int[] selectedIndices = list1.getSelectedIndices();
 	 				for(int a : selectedIndices)
 	 					System.out.println(a);
 	 				Thread t = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							for(int i = 0; i < selectedIndices.length; i++){
		 	 					try {
		 							//MergeAndSplit.splitPDF("E:/many1.pdf", "E:/many6.pdf", 2, 10);
		 							System.out.println(filepath1[selectedIndices[i]].replace("\\", "/"));
									System.out.println( s + fileName1[selectedIndices[i]].replace(".", "Splited") + i + ".pdf");
									MergeAndSplit.splitPDF(filepath1[selectedIndices[i]].replace("\\", "/"), (s + "\\" + fileName1[selectedIndices[i]].replace(".", "Splited") + i + ".pdf").replace("\\", "/"), Integer.valueOf(txtFromPage.getText()), Integer.valueOf(txtEndPage.getText()));
									
		 						} catch (NumberFormatException | IOException | DocumentException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
		 	 				}
						}
					});
 	 				t.start();
 				}
 			}
 		});
		btnToPDF.setBorder(null);
		btnToPDF.setBounds(40, 280, 300, 40);
 		splitPane.add(btnToPDF);
    }
    
    //PDF转图片
    public void pdfToImage(){
    	toImagePane =new JPanel();
    	toImagePane.setLayout(null);
    	toImagePane.setBounds(0, 20, 400, 410);
    	mainPane.add(toImagePane);
    	
		 JLabel lblplay;
	     lblplay = new JLabel("PDF文件列表：");
	     lblplay.setFont(new Font("宋体", Font.PLAIN, 15));
	     lblplay.setBounds(20, 10, 100, 30);
	     toImagePane.add(lblplay);
	     
	     JScrollPane ScrollList;
	     ScrollList = new JScrollPane(list2);
	     ScrollList.setBounds(20, 35, 340, 130);
	     ScrollList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	     ScrollList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	//         ScrollList.setOpaque(false);
	//         ScrollList.getViewport().setOpaque(false);
	     toImagePane.add(ScrollList);
	     
	     list2.setBackground(Color.lightGray);
	     // 允许可间断的多选
	     list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	     list2.addListSelectionListener(new ListSelectionListener() {
	         @Override
	         public void valueChanged(ListSelectionEvent e) {
	             // 获取所有被选中的选项索引
	             int[] indices = list2.getSelectedIndices();
	             // 获取选项数据的 ListModel
	             ListModel<String> listModel = list2.getModel();
	             // 输出选中的选项
	             for (int index : indices) {
	                 System.out.println("选中: " + index + " = " + listModel.getElementAt(index));
	             }
	             System.out.println();
	         }
	     });
	//         mergePane.add(list);
	     
	    //全选按钮
	    JButton btnSelectedAll;
	    btnSelectedAll = new JButton("全   选");
 		btnSelectedAll.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int[] a  = new int[MAX_NUMBER_OF_FILE];
 				for(int i = 0; i < index2; i++){
 					a[i] = i;
 				}
 				list2.setSelectedIndices(a);
 			}
 		});
 		btnSelectedAll.setBorder(null);
 		btnSelectedAll.setBounds(137, 175, 105, 40);
 		toImagePane.add(btnSelectedAll);
 		
 		//选择文件按钮
 		JButton btnAddFile;
 		btnAddFile = new JButton("添加文件");
 		btnAddFile.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int result = 0;
 				String path = null;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("请选择要上传的文件...");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					File[] files = fileChooser.getSelectedFiles();
 					for(File file:files)
                     {
                        filepath2[index2] = file.getAbsolutePath();
                        fileName2[index2] = file.getName();
                        index2++;
                     }
 					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
 					for(int i = 0; i < index2; i++){
 						fileNameList[i] = (i+1) + "." + fileName2[i];
 					}
                    list2.setListData(fileNameList);	
 		    	   path=fileChooser.getSelectedFile().getPath();
 		    	   System.out.println("path: "+path);
 				}
 			}
 		});
 		btnAddFile.setBorder(null);
 		btnAddFile.setBounds(27, 175, 105, 40);
 		toImagePane.add(btnAddFile);
 		
 		//删除文件按钮
 		JButton btnDeleteFile;
 		btnDeleteFile = new JButton("删除文件");
 		btnDeleteFile.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int[] selectedIndices = list2.getSelectedIndices();
 				for(int a : selectedIndices)
 					System.out.println(a);
 				for(int k = selectedIndices.length - 1; k >= 0; k--){
 					System.out.println(fileName2[k]);
 					for(int i = selectedIndices[k]; i < index2-1; i++){
 						filepath2[i] = filepath2[i+1];
 						fileName2[i] = fileName2[i+1];
 					}
 					index2--;
 					System.out.println(k);
 				}
 				String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
 				for(int i = 0; i < index2; i++){
 					fileNameList[i] = (i+1) + "." + fileName2[i];
 				}
                list2.setListData(fileNameList);	
 			}
 		});
 		btnDeleteFile.setBorder(null);
 		btnDeleteFile.setBounds(247, 175, 105, 40);
 		toImagePane.add(btnDeleteFile);
 		
 		JLabel lblplay1;
 		lblplay1 = new JLabel("保存到：");
 		lblplay1.setFont(new Font("宋体", Font.PLAIN, 15));
 		lblplay1.setBounds(20, 235, 70, 30);
 		toImagePane.add(lblplay1);
        
        JTextField txtOutputPath;
        txtOutputPath = new JTextField();
		txtOutputPath.setFont(new Font("宋体", Font.PLAIN, 12));
		txtOutputPath.setBackground(Color.LIGHT_GRAY);
		txtOutputPath.setForeground(Color.BLACK);
		txtOutputPath.setBounds(80, 235, 190, 30);
		toImagePane.add(txtOutputPath);
        
		//选择文件夹按钮
 		JButton btnAddFile1;
 		btnAddFile1 = new JButton("选  择");
 		btnAddFile1.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int result = 0;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("选择图片保存位置");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					String s = fileChooser.getSelectedFile().getAbsolutePath();
 					System.out.println(s);
 					txtOutputPath.setText(s);
 				}
 			}
 		});
 		btnAddFile1.setBorder(null);
 		btnAddFile1.setBounds(280, 232, 80, 35);
 		toImagePane.add(btnAddFile1);
		
		JButton btnToPDF;
		btnToPDF = new JButton("导 出 图 片 到 文 件 夹");
		btnToPDF.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int[] selectedIndices = list2.getSelectedIndices();
 				for(int a : selectedIndices)
 					System.out.println(a);
 				Thread t = new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						for(int i = 0; i < selectedIndices.length; i++){
		 					PDFAndImage p = new PDFAndImage();
							p.setInputPDFPath(filepath2[selectedIndices[i]].replace("\\", "/"));
							p.setOutputImagesLocation((txtOutputPath.getText() + "\\").replace("\\", "/"));
							p.pdfToImage(fileName2[selectedIndices[i]].replace(".", "New"));
		 				}
					}
				});
 				t.start();
 			}
 		});
		btnToPDF.setBorder(null);
		btnToPDF.setBounds(40, 280, 300, 40);
		toImagePane.add(btnToPDF);
    }
    
    //图片转PDF
    public void imageToPDF(){
    	toPDFPane =new JPanel();
    	toPDFPane.setLayout(null);
    	toPDFPane.setBounds(0, 20, 400, 410);
        mainPane.add(toPDFPane);
        
        JLabel lblplay;
        lblplay = new JLabel("图片文件列表：");
        lblplay.setFont(new Font("宋体", Font.PLAIN, 15));
        lblplay.setBounds(20, 10, 120, 30);
        toPDFPane.add(lblplay);
        
        JScrollPane ScrollList;
        ScrollList = new JScrollPane(list3);
        ScrollList.setBounds(20, 35, 200, 250);
        ScrollList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        ScrollList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        ScrollList.setOpaque(false);
//        ScrollList.getViewport().setOpaque(false);
        toPDFPane.add(ScrollList);
        
        list3.setBackground(Color.lightGray);
        // 允许可间断的多选
        list3.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list3.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                // 获取所有被选中的选项索引
                int[] indices = list3.getSelectedIndices();
                // 获取选项数据的 ListModel
                ListModel<String> listModel = list3.getModel();
                // 输出选中的选项
                for (int index : indices) {
                    System.out.println("选中: " + index + " = " + listModel.getElementAt(index));
                }
                System.out.println();
            }
        });
//        mergePane.add(list);
        
        //全选按钮
        JButton btnSelectedAll;
        btnSelectedAll = new JButton("全   选");
		btnSelectedAll.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] a  = new int[MAX_NUMBER_OF_FILE];
				for(int i = 0; i < index3; i++){
					a[i] = i;
				}
				list3.setSelectedIndices(a);
			}
		});
		btnSelectedAll.setBorder(null);
		btnSelectedAll.setBounds(250, 80, 105, 40);
		toPDFPane.add(btnSelectedAll);
		
		//选择文件按钮
		JButton btnAddFile;
		btnAddFile = new JButton("添加文件");
		btnAddFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int result = 0;
				String path = null;
				JFileChooser fileChooser = new JFileChooser();
				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
				fileChooser.setMultiSelectionEnabled(true);
				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
				fileChooser.setDialogTitle("请选择要上传的文件...");
				fileChooser.setApproveButtonText("确定");
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				result = fileChooser.showOpenDialog(mainPane);
				if (JFileChooser.APPROVE_OPTION == result) {
					File[] files = fileChooser.getSelectedFiles();
					for(File file:files)
                    {
                       filepath3[index3] = file.getAbsolutePath();
                       fileName3[index3] = file.getName();
                       index3++;
                    }
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index3; i++){
						fileNameList[i] = (i+1) + "." + fileName3[i];
					}
                   list3.setListData(fileNameList);	
		    	   path=fileChooser.getSelectedFile().getPath();
		    	   System.out.println("path: "+path);
				}
			}
		});
		btnAddFile.setBorder(null);
		btnAddFile.setBounds(250, 30, 105, 40);
		toPDFPane.add(btnAddFile);
		
		//删除文件按钮
		JButton btnDeleteFile;
		btnDeleteFile = new JButton("删除文件");
		btnDeleteFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] selectedIndices = list3.getSelectedIndices();
				for(int a : selectedIndices)
					System.out.println(a);
				for(int k = selectedIndices.length - 1; k >= 0; k--){
					System.out.println(fileName3[k]);
					for(int i = selectedIndices[k]; i < index3-1; i++){
						filepath3[i] = filepath3[i+1];
						fileName3[i] = fileName3[i+1];
					}
					index3--;
					System.out.println(k);
				}
				String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
				for(int i = 0; i < index3; i++){
					fileNameList[i] = (i+1) + "." + fileName3[i];
				}
               list3.setListData(fileNameList);	
			}
		});
		btnDeleteFile.setBorder(null);
		btnDeleteFile.setBounds(250, 130, 105, 40);
		toPDFPane.add(btnDeleteFile);
		
		//上移按钮
		JButton btnMoveUp;
		btnMoveUp = new JButton("上   移");
		btnMoveUp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int selectedIndex = list3.getSelectedIndex();
				if(selectedIndex > 0){
					String fp = filepath3[selectedIndex];
					filepath3[selectedIndex] = filepath3[selectedIndex - 1];
					filepath3[selectedIndex - 1] = fp;
					String fn = fileName3[selectedIndex];
					fileName3[selectedIndex] = fileName3[selectedIndex - 1];
					fileName3[selectedIndex - 1] = fn;
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index3; i++){
						fileNameList[i] = (i+1) + "." + fileName3[i];
					}
	               list3.setListData(fileNameList);	
	               list3.setSelectedIndex(selectedIndex - 1);
				}
			}
		});
		btnMoveUp.setBorder(null);
		btnMoveUp.setBounds(250, 180, 105, 40);
		toPDFPane.add(btnMoveUp);
		
		//下移按钮
		JButton btnMoveDown;
		btnMoveDown = new JButton("下   移");
		btnMoveDown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int selectedIndex = list3.getSelectedIndex();
				if(selectedIndex != index3-1){
					String fp = filepath3[selectedIndex];
					filepath3[selectedIndex] = filepath3[selectedIndex + 1];
					filepath3[selectedIndex + 1] = fp;
					String fn = fileName3[selectedIndex];
					fileName3[selectedIndex] = fileName3[selectedIndex + 1];
					fileName3[selectedIndex + 1] = fn;
					String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
					for(int i = 0; i < index3; i++){
						fileNameList[i] = (i+1) + "." + fileName3[i];
					}
	               list3.setListData(fileNameList);	
	               list3.setSelectedIndex(selectedIndex + 1);
				}
			}
		});
		btnMoveDown.setBorder(null);
		btnMoveDown.setBounds(250, 230, 105, 40);
		toPDFPane.add(btnMoveDown);
		

		JLabel lblplay1;
 		lblplay1 = new JLabel("新文件名：");
 		lblplay1.setFont(new Font("宋体", Font.PLAIN, 15));
 		lblplay1.setBounds(20, 295, 100, 30);
 		toPDFPane.add(lblplay1);
        
        JTextField txtNewName;
        txtNewName = new JTextField();
		txtNewName.setFont(new Font("宋体", Font.PLAIN, 15));
		txtNewName.setBackground(Color.LIGHT_GRAY);
		txtNewName.setForeground(Color.BLACK);
		txtNewName.setBounds(100, 295, 120, 30);
		toPDFPane.add(txtNewName);
		
		//导出按钮
		JButton btnExport;
        btnExport = new JButton("导出PDF");
        btnExport.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int result = 0;
 				JFileChooser fileChooser = new JFileChooser();
 				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
 				System.out.println(fsv.getHomeDirectory());                //得到桌面路径
 				fileChooser.setMultiSelectionEnabled(true);
 				fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 				fileChooser.setDialogTitle("选择将合成的PDF文件导出到");
 				fileChooser.setApproveButtonText("确定");
 				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 				result = fileChooser.showOpenDialog(mainPane);
 				if (JFileChooser.APPROVE_OPTION == result) {
 					String s = fileChooser.getSelectedFile().getAbsolutePath() + "\\";
 					System.out.println(s);
 					PDFAndImage p = new PDFAndImage();
 					int[] selectedIndices = list3.getSelectedIndices();
 	 				for(int a : selectedIndices)
 	 					System.out.println(a);
 	 				for(int i = 0; i < selectedIndices.length; i++){
 	 					try {
							p.addImage(filepath3[selectedIndices[i]]);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
 	 				}
 					p.setOutputPDFPath(s + txtNewName.getText() + ".pdf");
 	 				Thread t = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							try {
								p.imageToPDF();
							} catch (DocumentException | IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
 	 				t.start();
 				}
			}
		});
        btnExport.setBorder(null);
        btnExport.setBounds(250, 295, 105, 40);
        toPDFPane.add(btnExport);
    }
    
    public void drag()
    {
        new DropTarget(mainPane,DnDConstants.ACTION_COPY_OR_MOVE,new DropTargetAdapter()
        {
            @Override
            public void drop(DropTargetDropEvent dtde)
            {
                try{
                    
                    if(dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
                     {
                        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                        List<File>list_1=(List<File>)(dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                        
//                        String temp="";
                        for(File file:list_1)
                         {
                        	if(1 == judge){
                        		filepath[index] = file.getAbsolutePath();
                                fileName[index] = file.getName();
                                dtde.dropComplete(true);
                                index++;
                        	}else if(2 == judge){
                        		filepath1[index1] = file.getAbsolutePath();
                                fileName1[index1] = file.getName();
                                dtde.dropComplete(true);
                                index1++;
                        	}else if(3 == judge){
                        		filepath2[index2] = file.getAbsolutePath();
                                fileName2[index2] = file.getName();
                                dtde.dropComplete(true);
                                index2++;
                        	}else if(4 == judge){
                        		filepath3[index3] = file.getAbsolutePath();
                                fileName3[index3] = file.getName();
                                dtde.dropComplete(true);
                                index3++;
                        	}
                            
                         }
                        String[] fileNameList = new String[MAX_NUMBER_OF_FILE];
                        if(1 == judge){
                        	for(int i = 0; i < index; i++){
        						fileNameList[i] = (i+1) + "." + fileName[i];
        					}
                           list.setListData(fileNameList);	
                        }else if(2 == judge){
                        	for(int i = 0; i < index1; i++){
        						fileNameList[i] = (i+1) + "." + fileName1[i];
        					}
                           list1.setListData(fileNameList);	
                        }else if(3 == judge){
                        	for(int i = 0; i < index2; i++){
        						fileNameList[i] = (i+1) + "." + fileName2[i];
        					}
                           list2.setListData(fileNameList);	
                        }else if(4 == judge){
                        	for(int i = 0; i < index3; i++){
        						fileNameList[i] = (i+1) + "." + fileName3[i];
        					}
                           list3.setListData(fileNameList);	
                        }
    					
                     }
                    
                    else
                     {
                        dtde.rejectDrop();
                     }
                    
                }catch(Exception e){e.printStackTrace();}
                
            }
            
            
        });
        
        
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new PDFtoolsFrame().setVisible(true);
        
    }

    
    
}